package com.ys.考试;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 描述: TODO
 *
 * @author CodeYang
 * @version 2021/1/20 21:35
 */
public class Socket04 {

    public static void main(String[] args) {
        // serverSocket code


    }
}

class  Server{
    public static void main(String[] args) {
        new Server();
    }

    public Server(){
        try (
                // 1: 注册服务
                ServerSocket serverSocket = new ServerSocket(2333);
                // 2: 监听服务
                Socket socket = serverSocket.accept();
                // 3: 获得 in out
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        ) {
//            System.out.println("conn --- server");
            // 4 : 获得客户端发送 MSG
            String msg = in.readLine();

            System.out.println(msg);

            // 5 : server witer client
            out.write("msg: server --->success ");
            out.newLine(); //结束标记
            out.flush();
//            in.close();
//            out.close();

        }
        catch (Exception e) {
            e.printStackTrace();
        }


    }

}

class Client{

    public static void main(String[] args) {
        new Client();
    }

    public Client(){
        try(
                // Socket 包装请求
                Socket socket = new Socket("127.0.0.1",2333);
                BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                BufferedWriter out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        )
         {
//             System.out.println("conn --- client");
             // 1 request ---> server
             out.write("msg: client --- request ");
             out.newLine(); // 结束标志
             out.flush(); // 字符流 out 几点 flush()


             // 2: 接收 回写 msg
             String info = in.readLine();
             System.out.println(info);
//             in.close();
//             out.close();

         }catch(Exception e){
            e.printStackTrace();
        }


    }

}